package club.controller;

import club.pojo.AdoptAnimal;
import club.pojo.Pet;
import club.pojo.User;
import club.service.AdoptAnimalService;
import club.util.Message;
import com.github.pagehelper.PageInfo;
import club.pojo.AdoptAnimal;
import club.service.AdoptAnimalService;
import club.util.Message;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/adopt")
public class AdoptAnimalController {
    @Resource
    private AdoptAnimalService adoptAnimalService;

    // 查询申请列表
    @RequestMapping("/apply")
    @ResponseBody
    public Message agree(String userName, @RequestParam("pn") Integer pageNum, Integer state) {
        // 每页显示的条数
        Integer pageSize = 5;
        if (pageNum == 0) {
            pageNum = 1; // 如果页码为0，则默认为第一页
        }
        // 调用service层方法获取申请列表信息
        PageInfo<AdoptAnimal> pageInfo = adoptAnimalService.allAdoptAnimal(userName, pageNum, pageSize, state);
        return Message.success().add("pageInfo", pageInfo);
    }

    // 创建领养申请
    @RequestMapping("/create")
    @ResponseBody
    public Message create(HttpSession session) {
        // 从session中获取当前用户信息和待领养宠物信息
        User user = (User) session.getAttribute("user");
        Pet pet = (Pet) session.getAttribute("pet");
        // 更新宠物状态为待领养
        pet.setState(1);
        // 创建领养申请对象，并设置相关信息
        AdoptAnimal adoptAnimal = new AdoptAnimal();
        adoptAnimal.setUserId(user.getId());
        adoptAnimal.setPetId(pet.getId());
        adoptAnimal.setAdoptTime(new Date());
        adoptAnimal.setState(1); // 初始状态为申请中
        // 调用service层方法创建领养申请
        adoptAnimalService.create(adoptAnimal);
        return Message.success();
    }

    // 查询所有申请列表
    @RequestMapping("/adopts")
    @ResponseBody
    public Message adopts(@RequestParam(required = false) String adoptTime, @RequestParam(required = false, value = "pn") Integer pageNum) {
        // 每页显示的条数
        int pageSize = 3;
        if (pageNum == null) {
            pageNum = 1; // 如果页码为null，则默认为第一页
        }
        // 调用service层方法获取所有申请列表信息
        PageInfo<AdoptAnimal> pageInfo = adoptAnimalService.all(adoptTime, pageNum, pageSize);
        return Message.success().add("pageInfo", pageInfo);
    }

    // 驳回申请
    @RequestMapping("/disAgree")
    @ResponseBody
    public Message disAgree(Integer id) {
        // 调用service层方法更新申请状态为驳回
        int update = adoptAnimalService.update(id, 0);
        if (update > 0) {
            return Message.success(); // 更新成功返回成功消息
        }
        return Message.fail(); // 更新失败返回失败消息
    }

    // 同意申请
    @RequestMapping("/agree")
    @ResponseBody
    public Message agree(Integer id) {
        System.out.println(id); // 打印申请ID，用于调试
        // 调用service层方法更新申请状态为同意
        int update = adoptAnimalService.update(id, 2);
        if (update > 0) {
            return Message.success(); // 更新成功返回成功消息
        }
        return Message.fail(); // 更新失败返回失败消息
    }
}